<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core"  prefix="c"%>
<%@ taglib prefix="f" uri="http://www.springframework.org/tags/form"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>

<form name = "addArticle" id = "form">
<!--   id, title, content, picture, channel_id, category_id, user_id, hits,
   hot, status, deleted, created, updated, commentCnt, articleType -->
  <div class="form-group">
     <label for="titleid">文章标题</label>
    <input type="text" name="title" class="form-control" id="titleid" aria-describedby="titleHelp">
    <small id="titleHelp" class="form-text text-muted">请输入文章标题 最少2个汉字 最多不超过200个.</small>
  </div>
  
  <div class = "row">
  	<div class = "col-md-6">
  		 <div class="form-group">
		    <label for="exampleFormControlSelect1">频道</label>
		    <select class="form-control" name ="channel_id" id="articleChannel" >
		    </select>
 		 </div>
  	</div>
  	<div class = "col-md-6">
  		 <div class="form-group">
		    <label for="exampleFormControlSelect1">类别</label>
		    <select class="form-control" name = "category_id" id="articleCatgory" > 
		    </select>
 		 </div>
 
  	</div>
  
  
  </div>
  
  <div class="form-group">
    <label for="exampleFormControlFile1">图片</label>
    <input type="file" class="form-control-file" id="imgFile">
  </div>
  
   <div class="mb-3">
	    <label for="validationTextarea">文章内容</label>
	    <textarea class="form-control is-invalid" name = "content"  id="validationTextarea" placeholder="请在这里输入文章内容" required></textarea>
	    <div class="invalid-feedback">
	      	请在这里输入文章内容
	    </div>
	  </div>
 
 
  <input type = "submit" class = "btn btn-primary" value = "发布文章"  onclick = "add()">
</form>

<script type="text/javascript">

	var editor1 ;
	
	$.post("/article/findChannels",function(msg){
		
		if(msg.id == 0){
			
			for(var i in msg.t){
				
				$("#articleChannel").append("<option value = "+msg.t[i].id+">"+msg.t[i].name+"</option>");	
			}	
		}
	},"json")
	
	$("#articleChannel").change(function(){
		
		var channelId = $(this).val()
		
		$("#articleCatgory").empty();
		
		 $.post("/article/findCategorys",{channelId:channelId},function(msg){
			if(msg.id == 0){
				
				for(var i in msg.t){
					
					$("#articleCatgory").append("<option value = "+msg.t[i].id+">"+msg.t[i].name+"</option>");	
				}	
			}
			
		},"json")
		
	} )
	
	function add(){
			var formdata=new FormData($("#form")[0]);
			
			formdata.append("content",editor1.html());
			
			
			 $.ajax({
		           type :'post',
		           url : '/article/addArticle',
		           data : formdata,
		           cache : false,
		           processData : false, // 不处理发送的数据，因为data值是Formdata对象，不需要对数据做处理
		           contentType : false, // 不设置Content-type请求头
		           success : function(msg){
		        	   if(msg.id==0){
		        		   alert('发表成功');
		        		   console.log(msg.id);
		        		   location = "/user/home";
		        		   
		        	   }else{
		        		   console.log(msg.id);
		        		   alert('发表失败')
		        		   
		        	   }
		           },
		           error : function(){ }
		       }) 
			
		}
		
		

	
	
	
	
	$(function(){
		KindEditor.ready(function(K) {
			
			editor1 = K.create('#validationTextarea', {
				cssPath : '/resources/js/kindeditor/plugins/code/prettify.css',
				uploadJson : '/file/uploads.do',
				fileManagerJson : 'dssd',
				allowFileManager : true,
				afterCreate : function() {
					var self = this;
					K.ctrl(document, 13, function() {
						self.sync();
						document.forms['form'].submit();
					});
					K.ctrl(self.edit.doc, 13, function() {
						self.sync();
						document.forms['form'].submit();
					});
				}
			});
			prettyPrint();
		});
		})



</script>

